/**
 * 角色管理维护页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_ROLE_INFO");
    $(".form-select2").addClass("width-100").select2().on("select2-close", function (e) {
        $(this).isValid();
    });
    $("#saveBtn").click(function(){
        save();
    });
    $("#goBackBtn, #headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/roleInfo/index");
    });
    initData();
});

function initData(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/roleInfo/detail",
        data: {
            uniqueNo: $('#uniqueNo').val()
        },
        success: function (data) {
            $("#roleCode").val(data.roleCode);
            $("#roleName").val(data.roleName);
            $("#roleTypeCode").select2("val", data.roleTypeCode);
            $("#remark").val(data.remark);
        },
        error: function(data){
            Alert.error("初始化数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    if($('#editForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: springUrl+"/web/roleInfo/update",
            data: {
                uniqueNo: $("#uniqueNo").val(),
                roleCode: $("#roleCode").val(),
                roleName: $("#roleName").val(),
                roleTypeCode : $('#roleTypeCode').val(),
                roleTypeName : $('#roleTypeCode').val()!="" ? $('#roleTypeCode option:selected').text() : "",
                remark: $("#remark").val()
            },
            success: function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/roleInfo/index");
                });
            },
            error: function(data){
                layer.close(index);
                Alert.error("数据保存失败!");
            }
        });
    }

}

